package com.example.administrator.yuejian_2.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.ClubCourseListAdapter;
import com.example.administrator.yuejian_2.bean.ClubCourseListBean;
import com.example.administrator.yuejian_2.interservice.ClubService;
import com.example.administrator.yuejian_2.interservice.Const;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class ClubCourseFragment extends Fragment {
    private TextView course_name;
    private TextView course_intro;
    private TextView course_money;
    private RecyclerView recyclerView;
    private ClubCourseListAdapter adapter;

    private int club_id;

    public ClubCourseFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_club_course, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Intent intent =getActivity().getIntent();
        club_id=intent.getIntExtra("club_id",0);
        recyclerView = view.findViewById(R.id.course_recyclerView);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ClubService service = retrofit.create(ClubService.class);
        Call<List<ClubCourseListBean>> call = service.courselist(club_id);
        call.enqueue(new Callback<List<ClubCourseListBean>>() {
            @Override
            public void onResponse(Call<List<ClubCourseListBean>> call, Response<List<ClubCourseListBean>> response) {
                if (getActivity() == null)
                    return;
                // course_name.setText(response.body().getCourse_name());
                adapter = new ClubCourseListAdapter(getActivity(),response.body());//adapter显示一条数据
                LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());//定义布局
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//定义布局
                recyclerView.setLayoutManager(layoutManager);//
                recyclerView.setAdapter(adapter);//调用适配器
                recyclerView.setItemAnimator(new DefaultItemAnimator());//调用item

            }

            @Override
            public void onFailure(Call<List<ClubCourseListBean>> call, Throwable t) {

            }
        });


    }
}
